clear all
set memo 100m
capture log close

local filename ModernDynamic

log using Dynamic, replace text

local output_dir draft
local output_tex1 `filename'.tex


cd "C:\Users\rujia\Dropbox\Replication\treatyports"

use data/TreatyPorts_BasePanel.dta, clear


qui tabu id, gen(idummy)
qui tabu year, gen(ydummy)


tsset id  year
bysort id: gen rank=_n
order rank
by id: gen AnnualGrowth=(hpopu[2]-hpopu[1])/hpopu[1]/44 if rank<=2
order AnnualGrowth
by id: replace AnnualGrowth=100*(hpopu[3]-hpopu[2])/hpopu[2]/31 if rank==3
by id: replace AnnualGrowth=100*(hpopu[4]-hpopu[3])/hpopu[3]/29 if rank==4
by id: replace AnnualGrowth=100*(hpopu[5]-hpopu[4])/hpopu[4]/30 if rank==5
by id: replace AnnualGrowth=100*(hpopu[6]-hpopu[5])/hpopu[5]/43 if rank==6
by id: replace AnnualGrowth=100*(hpopu[7]-hpopu[6])/hpopu[6]/11 if rank==7
by id: replace AnnualGrowth=100*(hpopu[8]-hpopu[7])/hpopu[7]/18 if rank==8
by id: replace AnnualGrowth=100*(hpopu[9]-hpopu[8])/hpopu[8]/8 if rank==9
by id: replace AnnualGrowth=100*(hpopu[10]-hpopu[9])/hpopu[9]/10 if rank==10

sum AnnualGrowth, de
gen post_cmc=(year>cmcyear)

replace coastal2=0 if coastal2==.

forvalues x=1(1) 10 {
gen latitude_`x'=latitude*ydummy`x'
gen longitude_`x'=longitude*ydummy`x'
gen landtax_`x'=landtax*ydummy`x'
gen silk_`x'=silk*ydummy`x'
gen tea_`x'=tea*ydummy`x'
gen grandcanal_`x'=dis_yunhe*ydummy`x'
gen taipingr_`x'=taipingrrebellion*ydummy`x'

}



label var AnnualGrowth "Annual Growth"
label var post_cmc "Post CMC"
local fe "idummy* ydummy*"

local control_fe "latitude_* longitude_* silk_* tea_* grandcanal_* taipingr_* landtax_*"


gen treatyports=1 if cmcyear<.
replace treatyports=0 if treatyports==.


drop if year<1953

gen d1953=1 if year==1953
replace d1953=0 if d1953==.

gen d1964=1 if year==1964
replace d1964=0 if d1964==.

gen d1982=1 if year==1982
replace d1982=0 if d1982==.

gen d1990=1 if year==1990
replace d1990=0 if d1990==.

gen d2000=1 if year==2000
replace d2000=0 if d2000==.


gen d1953_treaty=treatyports*d1953
gen d1964_treaty=treatyports*d1964
gen d1982_treaty=treatyports*d1982
gen d1990_treaty=treatyports*d1990
gen d2000_treaty=treatyports*d2000

label var d1953_treaty "Treatyport*Year1953"
label var d1964_treaty "Treatyport*Year1964"
label var d1982_treaty "Treatyport*Year1982"
label var d1990_treaty "Treatyport*Year1990"
label var d2000_treaty "Treatyport*Year2000"

#delimit ;
eststo clear;

eststo: regress AnnualGrowth d1953_treaty d1982_treaty d1990_treaty d2000_treaty  `fe', noconstant vce(cluster id);
estadd local fe "Y";
estadd scalar rsqure=e(r2);
estadd scalar nobs=e(N);


eststo: regress AnnualGrowth d1953_treaty  d1982_treaty d1990_treaty d2000_treaty `fe'  `control_fe', noconstant vce(cluster id);
estadd local fe "Y";
estadd local control_fe "Y";
estadd scalar rsqure=e(r2);
estadd scalar nobs=e(N);

eststo:  regress AnnualGrowth d1953_treaty  d1982_treaty  d1990_treaty d2000_treaty `fe'  `control_fe' if wave!=1, noconstant vce(cluster id);
estadd local fe "Y";
estadd local control_fe "Y";
estadd scalar rsqure=e(r2);
estadd scalar nobs=e(N);


esttab using draft/`output_tex1', replace
	width(\hsize) 
	mlabel (, depvar)
	b(3)
	se(3)
	noconstant
	star(* 0.10 ** 0.05 *** 0.01)
	keep(
d1953_treaty 
 d1982_treaty 
d1990_treaty 
d2000_treaty
		)
		order(
d1953_treaty 
 d1982_treaty 
d1990_treaty 
d2000_treaty

)
		
	label
	stats(
		fe
		control_fe
		nobs
		rsquare, 
		labels(
			"Prefecture and Year FE" 
			"Controls*Year FE" 
			
			"\# observations"
			"R-squred"
			) 
		fmt(0)
		layout(@)
		)
	nonotes
	;
